
import { login } from "@/api/auth.js";
import AES from '@/components/AES/AES'
import userInfoList from "@/views/setting/users/userInfo.json"
import {
  setToken,
  removeToken
} from '@/utils/auth'

const state = {
};
const mutations = {
  SET_USERINFO: (state, info) => {
    state.userInfo = info
  },
};
const actions = {
  // 登录
  userLogin({
    commit
  }, userInfo) {
    return new Promise((resolve, reject) => {
      let currentUserInfo = userInfoList.rows.filter(row => {
        return row.userName == userInfo.username
      })
      // const params = {
      //   userName: userInfo.username.trim(),
      //   userPassword:AES.encrypt(JSON.stringify(userInfo.password))
      // }
      // login(params).then(res => {
      let res = {
        token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTcxMzQ5MDgwMSwiZXhwIjoxNzE0MDk1NjAxfQ.ZE3CMYUQSjtaA7XgtqhPGnyQbqRBKc5p_bBWwW_bntdwNIDltrh5BH-HZrOPRwahTV4J8IjMIP41rHLo2D59DA',
        data: currentUserInfo
      }
      setToken(res.token)
      localStorage.setItem("userInfo", JSON.stringify(res.data));
      resolve()
      // }).catch(error => {
      //   reject(error)
      // })
    })
  },


  // 前端 登出
  FedLogOut() {
    return new Promise(resolve => {
      localStorage.removeItem('userInfo')
      removeToken()
      location.reload()// 为了重新实例化vue-router对象 避免bug
      resolve()
    })
  }
};

export default {
  state,
  mutations,
  actions,
};
